package cn.erp.app.depot.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.erp.app.service.BaseInfoService;
import cn.erp.app.util.EuiRender;
import cn.erp.common.model.SvcBaseinfo;
import cn.erp.common.model.SvcRecord;

import com.jfinal.common.controller.BaseController;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;
@ControllerBind(controllerKey="/depot/stock",viewPath="/depot/stock")
public class StockController extends BaseController{
	public void index() {
		//所属类型，名称，规格
		List<SvcBaseinfo> baseInfo = BaseInfoService.me.getAllMateriel();
		setAttr("materiel", baseInfo);
		render("stock_index.html");
	}
	
	public void getdata(){
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		SvcRecord svc = getModel(SvcRecord.class, "inorout");

		Table table = TableMapping.me().getTable(SvcRecord.class);

		String sql = "select r.*,((case when sum(r1.num) is null then 0 else sum(r1.num) end)-(case when sum(r2.num) is null then 0 else sum(r2.num) end)) as count";
		String from = " from svc_record r "+
"left join svc_record r1 on r1.m_p_id = r.m_p_id and r1.id = r.id and r1.flag='001' or r1.flag='003'"+
"left join svc_record r2 on r2.m_p_id = r.m_p_id and r1.id = r.id  and r2.flag='002' where  1 = 1 ";

		List<Object> args = new ArrayList<Object>();
		Map<String, Class<?>> columnmap = table.getColumnTypeMap();

		for (Map.Entry<String, Class<?>> entry : columnmap.entrySet()) {
			String cname = entry.getKey();
			Class<?> type = entry.getValue();
			Object val = svc.get(cname);
			if (val != null) {
					from += " and  r." + cname + " =  ? ";
					args.add(val);
			}
		}
		from += " group by r.m_p_id,r.categoryId";

		Page<SvcRecord> result = SvcRecord.dao.paginate(pageNumber, pageSize, sql, from, args.toArray());

		render(EuiRender.page(result));
	}
	
	public void openadd(){
		
	}
	
	public void saveorupdate(){
		
	}
	
	public void delete(){
		
	}
	
	public void edit(){
		
	}
}
